package study.springcloud.config.nacos.support.ds;

import com.alibaba.druid.pool.DruidDataSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * （★） DataSource 动态刷新
 * （1）重写 DruidAbstractDataSource，注释掉 url 和 username 不可修改逻辑
 * （2）@ConfigurationProperties 实现参数动态刷新
 * （3）@RefreshScope注解方法，@Bean注解方法，实现 DataSource 实例化动态刷新
 */
@Slf4j
@Configuration
public class DataSourceConfig {

    @RefreshScope
    @Bean
    @ConfigurationProperties(prefix = "spring.ds.druid")
    public DruidDataSource dataSource() {
        log.info(">>>>>>");
        log.info(">>>>>> 实例化 DataSource");
        log.info(">>>>>>");
        DruidDataSource dataSource = new DruidDataSource();
        return dataSource;
    }
}
